# -*- encoding: utf-8 -*-
"""
File Name: redis_problems_publisher
Description:
Author : 'lirui'
date: 8/8/2024
Contact: marjey_lee@163.com
Change Activity:
8/8/2024:
"""
import json

import ll
from ll.third_party.redis_util.redis_publish import RedisPublic
from projects.monitoring.common.config.redis import REDIS_SERVER_INFO


class RedisProblemsPublishersManager:
    @staticmethod
    def _create_publisher(key):
        ip = REDIS_SERVER_INFO['host']
        port = REDIS_SERVER_INFO['port']
        p = RedisPublic(host=ip, port=port, channel=key)
        return p

    @classmethod
    def publish(cls, key, content):
        """

        Args:
            key:
            content:

        Returns:

        """
        try:
            p = cls._create_publisher(key)
            if not isinstance(content, str):
                content = json.dumps(content)
            p.publish(content)
            p.close()
        except Exception as e:
            ll.get_logger().error(f'Failed to publish problem to redis, key:{key},{e} content:{content}, ')
